package com.blog.boot.webapp.controller.common;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.api.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : cailong
 * @since : Created in 上午 10:36 2018/12/20 0020
 */
@Controller
public class UController extends BaseController {
    @Autowired
    UserService userService;

    @RequestMapping("/u/{id}.html")
    public RestMsg.RestModelAndView notFun(@PathVariable("id") Long id,HttpServletRequest request) {
        if( StringUtils.isEmpty(id) ){
            return restFactory.setModelAndView("forward:/error/404.html");
        }
        UserEntity user = userService.findUserByUserId(id);
        if( user == null ){
            return restFactory.setModelAndView("forward:/error/404.html");
        }
        request.setAttribute("u_user",user);
        return restFactory.setModelAndView("/index/views/user/home.html");
    }
    @RequestMapping("/find")
    public RestMsg.RestModelAndView findUsername(String username) {
        if( StringUtils.isEmpty(username) ){
            return restFactory.setModelAndView("forward:/error/404.html");
        }
        UserEntity user = userService.findUserByUserName(username);
        if( user == null ){
            return restFactory.setModelAndView("forward:/error/404.html");
        }
        return restFactory.setModelAndView("redirect:/u/"+user.getId()+".html");
    }

}
